package com.tianyi.url;

import com.tianyi.env.EnvService;
import com.tianyi.util.CommUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Spliterator;

/**
 * url service
 *
 * @author cuixuhui
 * @date 2020/05/07
 */
@Service
public class UrlService {

    private static final Logger logger = LoggerFactory.getLogger(UrlService.class);

    @Value("${exclude_url}")
    private String excludeUrl;
    @Value("${tianyi_url}")
    private String appUrl;

    @Autowired
    private EnvService envService;

    public static List<String> EXCLUDE_URLS=null;

    public List<String> getExcludeUrl(){
        if (CollectionUtils.isNotEmpty(EXCLUDE_URLS)){
            return EXCLUDE_URLS;
        }
        List<String> toList = CommUtils.COMMA_SPLITTER.splitToList(excludeUrl);
        EXCLUDE_URLS = toList;
        return EXCLUDE_URLS;
    }

    public String getCurrentLoginUrl(){
        return appUrl+"/login.html";
    }

    public boolean isExcludeUri(String uri){
        if (this.getExcludeUrl().contains(uri) || StringUtils.equals("/", uri)){
            return true;
        }
        return false;
    }

    public boolean isExcludeUrl(String url){

        if (StringUtils.isBlank(url)){
            return false;
        }
        List<String> excludeUrl = this.getExcludeUrl();
        String[] split = url.split("/");
        for (String uri : split) {
            if (excludeUrl.contains(uri)){
                return true;
            }
        }
        return false;
    }

}
